package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.OmcAdvisoryCancelMapper;
import com.csun.cmny.provider.dto.AdvisoryCancelQueryDto;
import com.csun.cmny.provider.exceptions.OmcBizException;
import com.csun.cmny.provider.model.domain.OmcAdvisory;
import com.csun.cmny.provider.model.domain.OmcAdvisoryCancel;
import com.csun.cmny.provider.pojo.Bed;
import com.csun.cmny.provider.service.BedService;
import com.csun.cmny.provider.service.OmcAdvisoryCancelService;
import com.csun.cmny.provider.service.OmcAdvisoryService;
import com.csun.cmny.provider.vo.AdvisoryCancelVo;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Service
@Transactional
public class OmcAdvisoryCancelServiceImpl extends BaseService<OmcAdvisoryCancel> implements OmcAdvisoryCancelService {

    @Resource
    private OmcAdvisoryCancelMapper omcAdvisoryCancelMapper;

    @Resource
    private OmcAdvisoryService omcAdvisoryService;

    @Resource
    private BedService bedService;

    @Override
    public Integer saveAdvisoryCancel(OmcAdvisoryCancel omcAdvisoryCancel, LoginAuthDto loginAuthDto) {

        omcAdvisoryCancel.setUpdateInfo(loginAuthDto);

        if(omcAdvisoryCancel.isNew()){

//            //查询床位信息
//            Bed bed = bedService.selectById(omcAdvisoryCancel.getBedId());
//            if(bed == null){
//                throw new OmcBizException(ErrorCodeEnum.OMC10034004);
//            }
//
//            //查找咨询记录
//            OmcAdvisory omcAdvisory = new OmcAdvisory();
//            omcAdvisory.setBedId(omcAdvisoryCancel.getBedId());
//            OmcAdvisory advisoryQuery = omcAdvisoryService.selectOne(omcAdvisory);
//            if(advisoryQuery == null){
//                throw new OmcBizException(ErrorCodeEnum.OMC10034001);
//            }
//
//            //将预约记录的床位置空
//            advisoryQuery.setBedId(null);
//            advisoryQuery.setRoomId(0);
//            int updateAdvisoryRet = omcAdvisoryService.updateAdvisory(advisoryQuery);
//            if(updateAdvisoryRet < 0){
//                throw new OmcBizException(ErrorCodeEnum.OMC10034005);
//            }
//
//            //床位状态 0-空床 1-暂出 2-有人  3-咨询入住时的预约 4-入住时的预约 5 - 试入住 6-调房预约
//            bed.setStatus(0);
//            int updateBedRet = bedService.updateById(bed);
//            if(updateBedRet < 0) {
//                throw new OmcBizException(ErrorCodeEnum.OMC10034003);
//            }

            //查询预约记录
            OmcAdvisory advisory = omcAdvisoryService.selectByKey(omcAdvisoryCancel.getAdvisoryId());
            if(advisory == null){
                throw new OmcBizException(ErrorCodeEnum.OMC10034001);
            }
            //取消床位预约
            if(advisory.getBedId() != null && advisory.getBedId() != 0){
                Bed bedQuery = new Bed();
                bedQuery = bedService.selectById(advisory.getBedId());
                if(bedQuery == null){
                    throw new OmcBizException(ErrorCodeEnum.OMC10034004);
                }
                bedQuery.setStatus(0);
                int retBed = bedService.updateById(bedQuery);
            }
            //将预约记录的床位置空
            advisory.setBedId(null);
            advisory.setRoomId(0);
            int updateAdvisoryRet = omcAdvisoryService.updateAdvisory(advisory);

            return omcAdvisoryCancelMapper.insertSelective(omcAdvisoryCancel);
        }else{
            return omcAdvisoryCancelMapper.updateByPrimaryKeySelective(omcAdvisoryCancel);
        }

    }

    @Override
    public PageInfo queryAdvisoryCancelListWithPage(AdvisoryCancelQueryDto advisoryCancelQueryDto) {

        PageHelper.startPage(advisoryCancelQueryDto.getPageNum(), advisoryCancelQueryDto.getPageSize());
        List<AdvisoryCancelVo> advisoryCancelList = omcAdvisoryCancelMapper.queryAdvisoryCancelListWithPage(advisoryCancelQueryDto);
        return new PageInfo<>(advisoryCancelList);
    }

    @Override
    public Integer deleteAdvisoryById(Long advisoryCancelId, LoginAuthDto loginAuthDto) {

        OmcAdvisoryCancel omcAdvisoryQuery = omcAdvisoryCancelMapper.selectByPrimaryKey(advisoryCancelId);
        if (PublicUtil.isEmpty(omcAdvisoryQuery)) {
            throw new OmcBizException(ErrorCodeEnum.OMC10034002);
        }
        return omcAdvisoryCancelMapper.deleteByPrimaryKey(advisoryCancelId);
    }
}
